package com.example.myapplication.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import coil.compose.rememberAsyncImagePainter
import com.example.myapplication.R

@Composable
fun ImageDemo(innerPadding: PaddingValues){
    //1.状态变量
    var result by remember { mutableIntStateOf(1) }

    //2.根据状态变量的值，动态的改变引用图片
    val imageResource = when (result) {
        1 -> R.drawable.dice_1
        2 -> R.drawable.dice_2
        3 -> R.drawable.dice_3
        4 -> R.drawable.dice_4
        5 -> R.drawable.dice_5
        else -> R.drawable.dice_6
    }

    Column(
        modifier = Modifier.fillMaxSize().padding(innerPadding),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(imageResource),
            contentDescription = "1",
            modifier = Modifier.size(120.dp)
                .clickable{
                    println("骰子被点击了")
                }
        )

        Image(
            painter = rememberAsyncImagePainter("https://img14.360buyimg.com/mobilecms/s360x360_jfs/t1/296269/13/6614/57790/6822bf23F1fab59f1/f93b1963ae7109a9.jpg!q70.dpg.webp"),
            contentDescription = "2",
        )

        Button(onClick = {
            result = (1..6).random()
        }) {
            Text(text = "掷骰子")
        }
    }
}